package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.OrderSettingVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(version = "1.0.0")
public class OrderSettingServiceImpl implements OrderSettingService {



    @Autowired
    private OrderSettingDao orderSettingDao;


    /**

     * 更新预约人数
     * @param reservations
     * @param formatDate
     * @return
     */
    @Override
    public int updateReservations(int reservations, String formatDate) {

        return orderSettingDao.updateOrderDate(reservations,formatDate);
    }


    /**
     * 修改预约最大人数
     *
     * @param orderDate
     * @param number
     */
    @Override

    public String update(String orderDate, Integer number) {

        //字符串转换时间
        Date date = null;
        try {
            date = DateUtils.parseString2Date(orderDate,"yyyy-MM-dd");

        } catch (Exception e) {
            e.printStackTrace();
        }

        //修改和添加的 设置按钮 共用一个路径
        //所以先判断这个前端传递的这个时间 有没有数据 有数据进行修改 没数据则添加
        OrderSetting result = orderSettingDao.findByTime(orderDate);


        if(result==null){
            OrderSetting orderSetting=new OrderSetting();
            //主键自增但不能为空..所以设个0
            orderSetting.setId(0);
            orderSetting.setNumber(number);
            orderSetting.setReservations(0);
            orderSetting.setOrderDate(date);

            orderSettingDao.add(orderSetting);
            return "ok";
        }


        if(number<result.getReservations()){
            return "请合理设置预约人数";
        }

        orderSettingDao.update(orderDate, number);
        return "OK";
    }



    /**

     * 查询挂号时间是否存在的方法
     *
     * @param date
     * @return
     */
    @Override
    public OrderSetting queryOrderDate(String date) {
        OrderSetting orderSetting = orderSettingDao.queryOrderDate(date);
        return orderSetting;
    }

    @Override
    @Transactional(readOnly = true)
    public List<OrderSettingVo> findAll(String orderDate) {

        //前端传递过来的2021-7 我们需要的是2021-07  切割成split[2021],split[7]
        String[] split = StringUtils.split(orderDate, "-");
        //如果月份小于10给月份前面加0
        if (Integer.valueOf(split[1]) < 10) {

            split[1] = "0" + split[1];
        }
        //调用dao查询模糊查询 查询条件为2021-07开头的  就能找出这个月所有的预约信息
        List<OrderSetting> OrderSettingList = orderSettingDao.findAll(split[0] + "-" + split[1]);

        //OrderSettingVo这个是前端要的格式  {[date:1 number:300 reservations:1]}
        //就因为前段不要id .... 自定义一个vo 封装
        List<OrderSettingVo> voList = new ArrayList<>();
        for (OrderSetting orderSetting : OrderSettingList) {

            OrderSettingVo orderSettingVo = new OrderSettingVo();
                                                      //这里如果getDay获得的是星期几 getDate才是获得当月的天数
            orderSettingVo.setDate(orderSetting.getOrderDate().getDate());
            orderSettingVo.setNumber(orderSetting.getNumber());
            orderSettingVo.setReservations(orderSetting.getReservations());
            voList.add(orderSettingVo);
        }

        return voList;
    }

    /**
     * 读取文件中的信息 批量修改或者上传预约数据
     * @param OrderSettings
     */
    @Override
    @Transactional
    public void upLoad(List<OrderSetting> OrderSettings) {

        //TODO 批量修改未实现

        for (OrderSetting orderSetting : OrderSettings) {
            orderSetting.setId(0);
            orderSetting.setReservations(0);
        }
        orderSettingDao.save(OrderSettings);
    }
}
